/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.i360day.invoker.socket;

import com.i360day.invoker.socket.handler.SimpleWebSocketEndpoint;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.DeploymentException;
import jakarta.websocket.server.ServerContainer;
import org.springframework.util.Assert;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.socket.server.standard.ServerEndpointRegistration;

import java.io.IOException;
import java.util.Collections;

/**
 * @author liju.z
 * @date 2024/1/30 20:56
 */
public class WebSocketHttpRequestHandler implements HttpRequestHandler {
    private final SimpleWebSocketEndpoint simpleWebSocketEndpoint;

    public WebSocketHttpRequestHandler(SimpleWebSocketEndpoint simpleWebSocketEndpoint) {
        this.simpleWebSocketEndpoint = simpleWebSocketEndpoint;
    }

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            ServerContainer container = getContainer(request);
            container.upgradeHttpToWebSocket(request, response, new ServerEndpointRegistration(request.getRequestURI(), simpleWebSocketEndpoint), Collections.emptyMap());
        } catch (DeploymentException e) {
            throw new RuntimeException(e);
        }
    }

    protected ServerContainer getContainer(HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        String attrName = "jakarta.websocket.server.ServerContainer";
        ServerContainer container = (ServerContainer) servletContext.getAttribute(attrName);
        Assert.notNull(container, "No 'jakarta.websocket.server.ServerContainer' ServletContext attribute. Are you running in a Servlet container that supports JSR-356?");
        return container;
    }
}
